#import "Common.h"
#import "DataType.h"
#import "LayerSelMode.h"
#import "LayerMainMenu.h"
#import "LayerGame.h"
#import "LayerStore.h"
#import "SpellSlot.h"
#import "Particle.h"

static const ccBlendFunc sBlendAdd = { GL_SRC_ALPHA, GL_ONE };
static const ccColor3B sBtnColor[2][2] =
	{ { ccc3(50,200,50), ccc3(100,250,100) },	{ ccc3(50,50,200), ccc3(100,100,250) } };
static const CGPoint sBtnPos[4] = { ccp(120,190), ccp(310,190), ccp(410,30), ccp(70,30) };
static const NSString* sBtnLabel[4] = { @"GMClassic", @"GMSurvival", @"BtnShop", @"BtnBack" };

@implementation SceneSelMode
-(id) init
{
	if ((self = [super init]))
		[self addChild:[LayerSelMode node]];
	return self;
}
@end

@implementation LayerSelMode

-(id) init
{
	if ((self = [super init])) {
		CCSprite* bg = [CCSprite spriteWithFile:@"background.png"];
		bg.position = gPointCenter;
		bg.opacity = 100;
		[self addChild:bg];
		
		ParticleSpace* par = [ParticleSpace node];
		[self addChild:par];

		// 标题
   		CCLabelTTF* title = [CCLabelTTF labelWithString:NSLocalizedString(@"SelModeTitle",nil)
											   fontName:gFontName
											   fontSize:30];
		title.position = ccp(240,280);
		title.color = ccGREEN;
		[self addChild:title];

		CCTexture2D* tex = [[CCTextureCache sharedTextureCache] addImage:@"btn-mode.png"];
		
		CCSprite* item;
		CCSprite* itemDown;
		CCLabelTTF* itemLabel;
		CCMenuItemSprite* menuItem;

		CCMenu* menu = [CCMenu menuWithItems:NULL];
		menu.position = CGPointZero;
		[self addChild:menu];
		
		for(int i=0; i<2; i++) {
			item = [CCSprite spriteWithTexture:tex];
			item.blendFunc = sBlendAdd;
			item.color = sBtnColor[i][0];
			item.scaleX = 1.2;
			itemDown = [CCSprite spriteWithTexture:tex];
			itemDown.blendFunc = sBlendAdd;
			itemDown.color = sBtnColor[i][1];
			itemDown.scaleX = 1.2;
			menuItem = [CCMenuItemSprite itemFromNormalSprite:item
											   selectedSprite:itemDown
													   target:self
													 selector:@selector(callMenu:)];
			menuItem.position = ccp(sBtnPos[i].x+gSettingData.startPosX,sBtnPos[i].y);
			itemLabel = [CCLabelTTF labelWithString:NSLocalizedString(sBtnLabel[i],nil)
										   fontName:gFontName
										   fontSize:26];
			itemLabel.color = ccc3(255,215,0);
			itemLabel.position = ccp(item.contentSize.width/2+13,item.contentSize.height/2+2);
			[menuItem addChild:itemLabel];
			[menu addChild:menuItem z:i+1000];
			
			// LeaderBoard
			item = [CCSprite spriteWithFile:@"btn-leaderboard.png"];
			itemDown = [CCSprite spriteWithFile:@"btn-leaderboard.png"];
			itemDown.color = ccGRAY;
			menuItem = [CCMenuItemSprite itemFromNormalSprite:item
											   selectedSprite:itemDown
													   target:self
													 selector:@selector(callMenu:)];
			menuItem.position = ccp(sBtnPos[i].x+gSettingData.startPosX-50,sBtnPos[i].y-50);
			menuItem.scale = 0.7;
			[menu addChild:menuItem z:i+2000];

			if(i==0)
				itemLabel = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"%s", commaprint(gGlobalData.highScoreTime)]
											   fontName:gFontName
											   fontSize:18];
			else
				itemLabel = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"%ds", gGlobalData.highScoreSurvival]
											   fontName:gFontName
											   fontSize:18];
			itemLabel.anchorPoint = gAnchorLeft;
			itemLabel.position = ccp(sBtnPos[i].x+gSettingData.startPosX-35, sBtnPos[i].y-50);
			itemLabel.color = ccYELLOW;
			[self addChild:itemLabel];
		}

		CCTexture2D* btn_02 = [[CCTextureCache sharedTextureCache] addImage:@"btn-02.png"];
		CCTexture2D* btn_02_s = [[CCTextureCache sharedTextureCache] addImage:@"btn-02-s.png"];

		// Back
		item = [CCSprite spriteWithTexture:btn_02];
		itemDown = [CCSprite spriteWithTexture:btn_02_s];
		menuItem = [CCMenuItemSprite itemFromNormalSprite:item
										   selectedSprite:itemDown
												   target:self
												 selector:@selector(callMenu:)];
		menuItem.position = sBtnPos[2];
		itemLabel = [CCLabelTTF labelWithString:NSLocalizedString(sBtnLabel[2],nil)
									   fontName:gFontName
									   fontSize:16];
		itemLabel.position = ccp(item.contentSize.width/2,item.contentSize.height/2+2);
		[menuItem addChild:itemLabel];
		[menu addChild:menuItem z:100];

		// Shop
		item = [CCSprite spriteWithTexture:btn_02];
		itemDown = [CCSprite spriteWithTexture:btn_02_s];
		menuItem = [CCMenuItemSprite itemFromNormalSprite:item
										   selectedSprite:itemDown
												   target:self
												 selector:@selector(callMenu:)];
		menuItem.position =sBtnPos[3];
		itemLabel = [CCLabelTTF labelWithString:NSLocalizedString(sBtnLabel[3],nil)
									   fontName:gFontName
									   fontSize:16];
		itemLabel.position = ccp(item.contentSize.width/2,item.contentSize.height/2+2);
		[menuItem addChild:itemLabel];
		[menu addChild:menuItem z:101];
		
		// Achievement
		item = [CCSprite spriteWithFile:@"btn-achievement.png"];
		itemDown = [CCSprite spriteWithFile:@"btn-achievement.png"];
		itemDown.color = ccGRAY;
		menuItem = [CCMenuItemSprite itemFromNormalSprite:item
										   selectedSprite:itemDown
												   target:self
												 selector:@selector(callMenu:)];
		menuItem.position = ccp(140,75);
		menuItem.scale = 0.7;
		[menu addChild:menuItem z:2002];
		
		NSString* strAchievement;
		if(gGCManager.finishedAmCount >= 0)
			strAchievement = [NSString stringWithFormat:@"%02d/21 %@", gGCManager.finishedAmCount, NSLocalizedString(@"Achievements",nil)];
		else
			strAchievement = [NSString stringWithFormat:@"??/21 %@", NSLocalizedString(@"Achievements",nil)];
		itemLabel = [CCLabelTTF labelWithString:strAchievement fontName:gFontName fontSize:18];
		itemLabel.anchorPoint = gAnchorLeft;
		itemLabel.position = ccp(160,75);
		itemLabel.color = ccYELLOW;
		[self addChild:itemLabel];
	}
	return self;
}

-(void) callMenu:(CCMenuItem *)sender
{
	int id = [sender zOrder];

	switch(id) {
		case 1000:		// Time Mode
			[[SimpleAudioEngine sharedEngine] playEffect:EF_GAMESTART];
			[[CCDirector sharedDirector] replaceScene:
			 [CCTransitionFade transitionWithDuration:0.5 scene:[[SceneGame alloc] initWithMode:GM_Time]]];
			break;
		case 1001:		// Survival Mode
			[[SimpleAudioEngine sharedEngine] playEffect:EF_GAMESTART];
			[[CCDirector sharedDirector] replaceScene:
			 [CCTransitionFade transitionWithDuration:0.5 scene:[[SceneGame alloc] initWithMode:GM_Survival]]];
			break;
		case 100:			// Store
		{
			[[SimpleAudioEngine sharedEngine] playEffect:SOUND_BTN01];
			[self onExit];
			LayerStore* store = [[[LayerStore alloc] initWithParent:self] autorelease];
			CCScene* scene = [[CCDirector sharedDirector] runningScene];
			[scene addChild:store z:30000];
		}
			break;
		case 101:			// Back
			[[SimpleAudioEngine sharedEngine] playEffect:SOUND_BTN01];
			[[CCDirector sharedDirector] replaceScene:
			 [CCTransitionFade transitionWithDuration:0.5 scene:[SceneMainMenu node]]];
			break;
		case 2000:		// Time Mode LeaderBoard
			[[SimpleAudioEngine sharedEngine] playEffect:SOUND_BTN01];
			[gGCManager showLeaderboard:1];
			break;
		case 2001:		// Survival Mode LeaderBoard
			[[SimpleAudioEngine sharedEngine] playEffect:SOUND_BTN01];
			[gGCManager showLeaderboard:2];
			break;
		case 2002:		// Achievement
			[[SimpleAudioEngine sharedEngine] playEffect:SOUND_BTN01];
			[gGCManager showAchievements];
			break;
	}
}

-(void)onEnter
{
	if(mSpell)
		[self removeChild:mSpell cleanup:true];
	mSpell = [SpellSlot node];
	[self addChild:mSpell];
	[super onEnter];
}

@end
